package com.quasar.hpatient.view.prescription;

import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.DrugsAdjustmentData;
import com.quasar.hpatient.model.custommodel.PharmacyRecordDataBean;
import com.quasar.hpatient.model.event.UpdateEvent;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.medicalmodel.PrescriptionDayData;
import com.quasar.hpatient.onInterface.AnotherResultListener;
import com.quasar.hpatient.presenter.EditmedicinePresenter;
import com.quasar.hpatient.utils.DateUtil;
import com.quasar.hpatient.view.adapter.PharmacyRecordAdapter;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 调药记录
 */
@EActivity(R.layout.activity_pharmacy_record)
public class PharmacyRecordActivity extends BaseActivity implements AnotherResultListener<AnotherResult<DrugsAdjustmentData>>, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    private int delayMillis = 1000;//设置刷新等待的时间
    private EditmedicinePresenter editmedicinePresenter;
    //判断下一页是否存在数据
    private boolean onLoadMore = false;
    private PharmacyRecordAdapter pharmacyRecordAdapter;

    @ViewById(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;
    private View errorView;

    int page = 1;
    @ViewById(R.id.rl_nodata)
    RelativeLayout rl_nodata;

    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        errorView = getLayoutInflater().inflate(R.layout.error_view, (ViewGroup) rv_list.getParent(), false);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rv_list.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    protected void initData() {
        editmedicinePresenter = new EditmedicinePresenter(this);
        initAdapter();
    }


    //初始化adapter布局
    public void initAdapter() {
        pharmacyRecordAdapter = new PharmacyRecordAdapter();
        pharmacyRecordAdapter.setOnLoadMoreListener(this, rv_list);
        rv_list.setAdapter(pharmacyRecordAdapter);
        editmedicinePresenter.QueryAllPrescriptionData(page + "");
    }

    //    @Override
    public void onSuccess(AnotherResult<DrugsAdjustmentData> data) {
        if (data.getList() != null && data.getList().size() > 0) {
            //对调药记录进行排序
            Collections.sort(data.getList(), new Comparator<DrugsAdjustmentData>() {
                @Override
                public int compare(DrugsAdjustmentData o1, DrugsAdjustmentData o2) {
                    Date date1 = DateUtil.stringToDatess(o1.getEditTime());
                    Date date2 = DateUtil.stringToDatess(o2.getEditTime());
                    if (date1.before(date2)) {
                        return 1;
                    }
                    return -1;
                }
            });

            if (data.getList().get(0) != null) {
                if (page == 1) {
                    pharmacyRecordAdapter.setNewData(data.getList());
                    onLoadMore = false;
                } else {
                    pharmacyRecordAdapter.addData(data.getList());
                    if (data.getList().size() == 0) {
                        onLoadMore = true;
                    }
                }
            }
        } else {
            onLoadMore = true;
        }

        pharmacyRecordAdapter.setEmptyView(errorView);

    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onRefreshUI() {
        pharmacyRecordAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        ViseLog.d("onRefresh");
        page = 1;
        editmedicinePresenter.QueryAllPrescriptionData(page + "");
        pharmacyRecordAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //下拉后添加新数据
                swipeLayout.setRefreshing(false);//下拉刷新关闭
                pharmacyRecordAdapter.setEnableLoadMore(true);
            }
        }, delayMillis);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;//下拉时page递增
        editmedicinePresenter.QueryAllPrescriptionData(page + "");
        rv_list.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断当返回的数据为空时停止下拉刷新
                if (onLoadMore) {
                    pharmacyRecordAdapter.loadMoreEnd();
                } else {
                    pharmacyRecordAdapter.loadMoreComplete();
                    pharmacyRecordAdapter.notifyDataSetChanged();
                }
            }

        }, delayMillis);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //刷新界面
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UpdateEvent updateEvent) {
        if (updateEvent.type.equals(ConstantManger.MYGUESS)) {
            initData();
            ViseLog.d("刷新home药单界面");
        }
    }
}
